package com.ruoyi.project.module.cjl;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.CxcyBaseinfo;
import com.ruoyi.project.domain.SysUserRole;
import com.ruoyi.project.service.ICxcyBaseinfoService;
import com.ruoyi.project.service.ISysRoleService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;
import static com.ruoyi.common.security.utils.SecurityUtils.getUserId;

/**
 * 人员信息Controller
 * 
 * @author ruoyi
 * @date 2023-05-10
 */
@RestController
@RequestMapping("/cjl/baseinfo")
public class CjlCxcyBaseinfoController extends BaseController
{
    @Autowired
    private ICxcyBaseinfoService cxcyBaseinfoService;
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询人员信息列表
     */
    @PostMapping("/ip")
    public String ip() throws Exception{
        InetAddress address = InetAddress.getLocalHost();  //获取本机IP地址
        String localIP = address.getHostAddress();  //获取本机IP地址字符串
        System.out.println("本机IP地址为：" + localIP);
        return localIP;
    }
    @RequiresPermissions("project:cjl:baseinfo:list")
    @GetMapping("/list")
    public TableDataInfo list(CxcyBaseinfo cxcyBaseinfo)
    {
        startPage();
        List<CxcyBaseinfo> list = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
        return getDataTable(list);
    }

    /**
     * 查询企业信息
     */
    @RequiresPermissions("project:cjl:baseinfo:enterlist")
    @GetMapping("/enterlist")
    public TableDataInfo enterlist(CxcyBaseinfo cxcyBaseinfo)
    {
        startPage();
        Long userId = getUserId();
        SysUserRole sysRole = sysRoleService.selectRoleByUser(userId);
        Long roleId = sysRole.getRoleId();
        if(roleId==1 || roleId==12){
            cxcyBaseinfo.setUserType("1");
            List<CxcyBaseinfo> Alllist = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
            return getDataTable(Alllist);
        }else if(roleId==13){
            cxcyBaseinfo.setUserId(getUserId());
            List<CxcyBaseinfo> Somelist = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
            return getDataTable(Somelist);
        }
        List<CxcyBaseinfo> list = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
        return getDataTable(list);
    }

    /**
     * 查询个人信息
     */
    @RequiresPermissions("project:cjl:baseinfo:personlist")
    @GetMapping("/personlist")
    public TableDataInfo personlist(CxcyBaseinfo cxcyBaseinfo)
    {
        startPage();
        Long userId = getUserId();
        SysUserRole sysRole = sysRoleService.selectRoleByUser(userId);
        Long roleId = sysRole.getRoleId();
        if(roleId==1 || roleId==12){
            cxcyBaseinfo.setUserType("2");
            List<CxcyBaseinfo> Alllist = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
            return getDataTable(Alllist);
        }else if(roleId==14){
            cxcyBaseinfo.setUserId(getUserId());
            List<CxcyBaseinfo> Somelist = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
            return getDataTable(Somelist);
        }
        List<CxcyBaseinfo> list = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
        return getDataTable(list);
    }
    /**
     * 导出企业信息列表
     */
    @RequiresPermissions("project:cjl:enterbaseinfo:export")
    @Log(title = "企业信息", businessType = BusinessType.EXPORT)
    @PostMapping("/enterexport")
    public void export(HttpServletResponse response, CxcyBaseinfo cxcyBaseinfo)
    {
        cxcyBaseinfo.setUserType("1");//查询企业
        List<CxcyBaseinfo> list = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
        ExcelUtil<CxcyBaseinfo> util = new ExcelUtil<CxcyBaseinfo>(CxcyBaseinfo.class);
        util.hideColumn("userId","nickName","sex","idNumber","userType","college",
                "major","studentNumber","grade","cardNumber","openingBank","bankAddress",
                "jobNumber","professionalTitl","note4","note5","note6","note7","note8","note9","note10"
        );
        util.exportExcel(response, list, "企业信息数据");
    }

    /**
     * 导出个人信息列表
     */
    @RequiresPermissions("project:cjl:personbaseinfo:export")
    @Log(title = "个人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/personexport")
    public void personexport(HttpServletResponse response, CxcyBaseinfo cxcyBaseinfo)
    {
        cxcyBaseinfo.setUserType("2");//查询个人
        List<CxcyBaseinfo> list = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
        ExcelUtil<CxcyBaseinfo> util = new ExcelUtil<CxcyBaseinfo>(CxcyBaseinfo.class);
        util.hideColumn("userId","nickName","sex","idNumber","userType","college",
                "major","studentNumber","grade","cardNumber","openingBank","bankAddress","enterpriseName",
                "enterpriseNumber","corporateName","corporateEmail","corporatePhone","corporateNumber","businessLicense",
                "licenseAddress","enterpriseAddress",
                "jobNumber","professionalTitl","note4","note5","note6","note7","note8","note9","note10"
        );
        util.exportExcel(response, list, "个人信息数据");
    }

    /**
     * 获取人员信息详细信息
     */
    @RequiresPermissions("project:cjl:baseinfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cxcyBaseinfoService.selectCxcyBaseinfoById(id));
    }

    /**
     * 新增人员信息
     */
   /* @RequiresPermissions("project:cjl:baseinfo:add")*/
    @Log(title = "人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CxcyBaseinfo cxcyBaseinfo)
    {
        return toAjax(cxcyBaseinfoService.insertCxcyBaseinfo(cxcyBaseinfo));
    }

    /**
     * 修改人员信息
     */
    @RequiresPermissions("project:cjl:baseinfo:edit")
    @Log(title = "人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CxcyBaseinfo cxcyBaseinfo)
    {
        return toAjax(cxcyBaseinfoService.updateCxcyBaseinfo(cxcyBaseinfo));
    }

    /**
     * 删除人员信息
     */
    @RequiresPermissions("project:cjl:baseinfo:remove")
    @Log(title = "人员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cxcyBaseinfoService.deleteCxcyBaseinfoByIds(ids));
    }

    /**
     * 审核成果
     */
    @RequiresPermissions("project:cjl:baseinfo:aduit")
    @Log(title = "审核信息", businessType = BusinessType.UPDATE)
    @PutMapping("/aduit")
    public AjaxResult aduit(@RequestBody CxcyBaseinfo cxcyBaseinfo) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        cxcyBaseinfo.setAuditName(sysUser.getNickName());
        cxcyBaseinfo.setAuditNumber(sysUser.getUserName());
        cxcyBaseinfo.setAuditTime(new Date());
        return toAjax(cxcyBaseinfoService.updateCxcyBaseinfo(cxcyBaseinfo));
    }
}
